﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DBSearch : System.Web.UI.Page
{
    private ProxyWrapper.UserProxy.UserDetails _objUser = null;
    private string DatabaseId = "0";
    private int _dataCount = 0;
    private bool isCaseSensitive = false;
    private string _dbName = "";
    private string searchQuery = "";
    public string _messagetype = "";
    public string _message = "";
  
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request["searchquery"] != null && !string.IsNullOrWhiteSpace(Request["searchquery"].ToString()))
            {
                searchQuery = Request["searchquery"].ToString();
            }
            int _dbId = 0;
            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                DatabaseId = Request.QueryString["dbid"] != null ? Request.QueryString["dbid"].ToString() : "0";
                _objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
                if (Request.QueryString != null && Request.QueryString["msg"] != null)
                {
                    _messagetype = "S";
                    _message = Request.QueryString["msg"].ToString();
                }
                if (Int32.TryParse(DatabaseId, out _dbId) && _dbId > 0)
                {
                   // LoadSearchData(Request.QueryString["search"] != null ? Request.QueryString["search"].ToString() : "");
                }
            }
        }
        catch (Exception ex)
        {

        }
    }

    private ProxyWrapper.CommonProxyService.Scans[] LoadSearchData(string searchQuery)
    {

        ProxyWrapper.DatabaseServiceWrapper _dbService = new ProxyWrapper.DatabaseServiceWrapper();
        ProxyWrapper.CommonProxyService.DatabaseDetails _dbDetails = _dbService.GetDBDetail(Convert.ToInt32(DatabaseId), _objUser.UserId);
        isCaseSensitive = _dbDetails.IsCaseSensitiveValidation;
        _dataCount = _dbDetails.RowCount;
        _dbName = _dbDetails.DBName;
        ProxyWrapper.CommonProxyService.Scans[] _data = _dbDetails.ScanDataList;
        FilterResult(ref _data);
       // lstScanData.DataSource = _data;
        //lstScanData.DataBind();
        return _data;
    }

    private void FilterResult(ref ProxyWrapper.CommonProxyService.Scans[] _data)
    {
        int studentNumber = 0;
        if (_data != null && _data.Length > 0)
        {
            if (Int32.TryParse(searchQuery, out studentNumber) && studentNumber > 0)
            {
                _data = _data.Where(m => m.StudentIdNumber == studentNumber).ToArray();
            }
            else if(!string.IsNullOrEmpty(searchQuery) && isCaseSensitive)
            {
                _data = _data.Where(m => m.FirstName.Contains(searchQuery) || m.LastName.Contains(searchQuery)).ToArray();
            }
            else if (!string.IsNullOrEmpty(searchQuery) && !isCaseSensitive)
            {
                _data = _data.Where(m => m.FirstName.ToUpper().Contains(searchQuery.ToUpper()) || m.LastName.ToUpper().Contains(searchQuery.ToUpper())).ToArray();
            }
        }
    }

    public string GetValidityImage(string responseCode)
    {
        string imageUrl = "Images/red.jpg";

        switch ((Enums.ValidityType)(Enum.Parse(typeof(Enums.ValidityType), responseCode)))
        {
            case Enums.ValidityType.Red:
                imageUrl = "Images/red.jpg";
                break;
            case Enums.ValidityType.Green:
                imageUrl = "Images/green.jpg";
                break;
            case Enums.ValidityType.Brown:
                imageUrl = "Images/brown.jpg";
                break;
            case Enums.ValidityType.Yellow:
                imageUrl = "Images/yellow.jpg";
                break;
            case Enums.ValidityType.blue:
                imageUrl = "Images/blue.jpg";
                break;
            default:
                imageUrl = "Images/red.jpg";
                break;
        }
        return imageUrl;
    }

    public string GetUserId()
    {
        return _objUser != null ? _objUser.UserId.ToString() : "0";
    }
    public string GetDatabaseName()
    {
        return _dbName;
    }
    public string GetDatabaseId()
    {
        return Request.QueryString["dbid"] != null ? Request.QueryString["dbid"].ToString() : "0";
    }
    public string GetCaseSensitiveCount()
    {
        if (isCaseSensitive)
            return _dataCount.ToString() + " Case-Sensitive Values";
        else
            return _dataCount.ToString() + " Values";
    }

    protected void dataPaging_PreRender(object sender, EventArgs e)
    {
        try
        {
            lstScanData.DataSource = LoadSearchData(Request.QueryString["search"] != null ? Request.QueryString["search"].ToString() : "");
            lstScanData.DataBind();
        }
        catch (Exception)
        {
        }
    }
    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (_messagetype == "E")
            return string.Format(warning, _message);
        else if (_messagetype == "S")
            return string.Format(result, _message);
        else
            return "";

    }
}